package cn.fudong.bos.service.take_delivery.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.fudong.bos.dao.take_delivery.WayBillRepository;
import cn.fudong.bos.domain.take_delivery.WayBill;
import cn.fudong.bos.index.WayBillIndexRepository;
import cn.fudong.bos.service.take_delivery.WayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {

	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;

	@Autowired
	private WayBillRepository wayBillRepository;

	// 保存运单
	@Override
	public void save(WayBill wayBill) {
		// 判断运单号是否存在
		WayBill posistwaybill = wayBillRepository.findByWayBillNum(wayBill.getWayBillNum());
		if (posistwaybill == null) {
			// 运单号不存在
			wayBill.setSignStatus(1);// 待发货(如果是新增的运单，则创建运单时，先将运行类型设置为默认：待发货）

		} else {
			// 运单号存在,实行更新操作

			/*
			 * try { BeanUtils.copyProperties(posistwaybill, wayBill); } catch
			 * (Exception e) { e.printStackTrace(); throw new
			 * RuntimeException(e.getMessage()); }
			 */
			wayBill.setId(posistwaybill.getId());

		}

		wayBillRepository.save(wayBill);
		// 保存数据时候保存索引
		wayBillIndexRepository.save(wayBill);
	}

	// 带条件条件分页查询
	@Override
	public Page<WayBill> findpageDate(WayBill wayBill, Pageable pageable) {
		// 判断里面是否有条件,如果没有条件,无条件分页查询
		if (StringUtils.isBlank(wayBill.getWayBillNum()) && StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress()) && StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
			// 无条件查询 、查询数据库
			return wayBillRepository.findAll(pageable);
		} else {
			// 如果有条件
			// 构造条件
			BoolQueryBuilder query = new BoolQueryBuilder();
			// 查询运单号
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				QueryBuilder termQuery = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
				query.must(termQuery);
			}
			// 查询发货地,模糊查询
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {

				// 第一种情况,使用ik分词器已经分词的的词条查询

				QueryBuilder wildcardQuery = new WildcardQueryBuilder("sendAddress",
						"*" + wayBill.getSendAddress() + "*");

				// 第二张情况,用语句查询
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getSendAddress())
						.field("sendAddress").defaultOperator(Operator.AND);

				// 两种情况取OR
				BoolQueryBuilder boolquery = new BoolQueryBuilder();
				boolquery.should(queryStringQueryBuilder);
				boolquery.should(wildcardQuery);

				query.must(boolquery);

			}
			// 查询收货地,模糊查询
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("recAddress",
						"*" + wayBill.getRecAddress() + "*");
				query.must(wildcardQuery);
			}
			// 查询快递产品类型sendProNum 等值查询
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
				query.must(termQuery);
			}
			// 查询签收状态signStatus
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				QueryBuilder termQuery = new TermQueryBuilder("signStatus", wayBill.getSignStatus());
				query.must(termQuery);
			}
			// 构造分页和条件
			SearchQuery searchQuery = new NativeSearchQuery(query);
			searchQuery.setPageable(pageable);
			// 调Dao层方法
			return wayBillIndexRepository.search(searchQuery);

		}

	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		return wayBillRepository.findByWayBillNum(wayBillNum);
	}
	
	//将wayBill索引与数据库同步
	@Override
	public void syncWayBillIndex() {
		List<WayBill> WayBills = wayBillRepository.findAll();
		wayBillIndexRepository.save(WayBills);
	}
	
	
	
	
}
